from pyflink.datastream import StreamExecutionEnvironment, RuntimeExecutionMode, MapFunction, FilterFunction, \
    KeySelector

# 1、创建flink执行环境
env = StreamExecutionEnvironment.get_execution_environment()

# 修改并行度
env.set_parallelism(1)

# 2、读取数据,得到DataStream，相当于RDD  （有界流）
lines_ds = env.read_text_file("G:\LanZhiPeiXun\Flink\data\students.txt")

# key_by: 将相同的key分到同一个分区中进行处理，一般keyby之后进行聚合计算
# key_by会产生shuffle

key_by_ds = lines_ds.key_by(lambda line: line.split(",")[-1])

# key_by_ds.print()


# 类和对象方式
class KeyByFunction(KeySelector):
    def get_key(self, line):
        return line.split(",")[-1]


lines_ds.key_by(KeyByFunction()).print()

env.execute()
